//
//  STLogsHomeController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
import WMZDialog

class STLogsHomeController: STProjectBaseController,UITextFieldDelegate {
    lazy var viewModel: STLogsViewModel = {
        return STLogsViewModel()
    }()
    @IBOutlet var totalView: UIView!
    lazy var ideaView: STAddIdeaView = {
        let view = Bundle.main.loadNibNamed("STAddIdeaView", owner: self, options: nil)?.first as! STAddIdeaView
        view.frame = keyWindow!.frame
        view.onSave = {
            self.getPageData()
        }
        return view
    }()
    var sort = false
    @IBOutlet var sortIcon: UIImageView!
    @IBOutlet var totalLabel: UILabel!
    @IBOutlet var searchTF: UITextField!
    @IBOutlet var addButton: UIButton!
    @IBOutlet var pageTab: UITableView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getPageData()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.addButton.addDashedBorder(color: .black, lineWidth: 1, cornerRadius: 15)
        self.totalView.addDashedBorder(color: .black, lineWidth: 1, cornerRadius: 15)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        self.searchTF.delegate = self
        setTab()
        
        // Do any additional setup after loading the view.
    }
    @IBAction func clickSortAction(_ sender: Any) {
        let originalArray = self.viewModel.dataSource
        self.viewModel.dataSource = Array(originalArray.reversed())
        self.sort = !self.sort
        self.sortIcon.image = UIImage(named: self.sort ? "icon_up" : "icon_sort")

        self.pageTab.reloadData()
    }
    
    func getPageData(){
        self.viewModel.loadData(self.searchTF.text!, onSuccess: { models in
            self.totalLabel.text = "\(models.count)"
            self.pageTab.reloadData()
        })
    }
    func setTab(){
        pageTab.dataSource = self
        pageTab.delegate = self
        pageTab.register(UINib(nibName: "STLogsListTableCell", bundle: .main), forCellReuseIdentifier: "STLogsListTableCell")
        
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        getPageData()
        self.view.endEditing(true)
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        getPageData()
        self.view.endEditing(true)
    }
    @IBAction func clickAddSomthingsAction(_ sender: Any) {
        self.navigationController?.pushViewController(STLogsAddController(), animated: true)
    }
   
}

extension STLogsHomeController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:STLogsListTableCell = tableView.dequeueReusableCell(withIdentifier: "STLogsListTableCell")  as! STLogsListTableCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.viewModel.dataSource[indexPath.row]
        if model.idea.isEmpty {
            self.ideaView.model = model
           keyWindow!.addSubview(self.ideaView)
        }else{
            let detailsVC = STLogsDetailsController()
            detailsVC.model = model
            self.navigationController?.pushViewController(detailsVC, animated: true)
        }
    }
    func addSub(){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "")
        label.frame = CGRect(x: 205, y: 431.5, width: 52, height: 13)
        label.numberOfLines = 0
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "ssa", size: 13) as Any,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
}
